package com.lzx.iteam;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.util.AllDialogUtil;

/**
 * Created by zhangshuai on 15-4-14.
 */
public class WebViewActivity extends Activity implements View.OnClickListener {

    private LinearLayout mBack;
    private TextView mTvTitle;
    private WebView mWebView;
    private AllDialogUtil mDialogUtil;
    private Dialog mDialog;
    /**
     * -2: 使用条款
     * -1: 联系我们
     * 0:帮助页面
     * 1:设置日程表管理员
     * 2:考勤点设置
     * 3:发审批
     * 4:工作平台
     * 5:发布公告
     * 6:发布活动
     * 7:发布投票
     * 8:发布会议通知
     * 9:发布事件
     * 10:帮别人安排日程
     * 11:创建新团队
     * 12:编辑组织架构
     * 13:团队管理
     * 14:人脉圈
     * 15:草稿箱
     * 16:审批人设置
     * 17:联系人详情
     * 18:发布周报
     * 19:设置周报阅览人
     * 20:自定义周报模板
     */
    private int type = 0;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help_layout);
        initView();
        initDatas();
    }

    public void initView() {
        mBack = (LinearLayout) findViewById(R.id.help_ll_back);
        mTvTitle = (TextView) findViewById(R.id.tv_help_name);
        mBack.setOnClickListener(this);
        mWebView = (WebView) findViewById(R.id.help_web_view);
        // 设置webView支持js
        WebSettings ws = mWebView.getSettings();
        ws.setJavaScriptEnabled(true);
        mDialogUtil = AllDialogUtil.getInstance(this);
        mDialog = mDialogUtil.waitDialog();
        mDialog.show();
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                if (newProgress == 100) {
                    mDialog.dismiss();
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    private void initDatas() {
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 0);
        title = intent.getStringExtra("title");
        mTvTitle.setText(title);
        switch (type) {
            case -2:
                mWebView.loadUrl(AsynHttpClient.HTTP_AGREEMENT);
                break;
            case -1:
                mWebView.loadUrl(AsynHttpClient.HTTP_CONTACT_US);
                break;
            case 0:
                mWebView.loadUrl(AsynHttpClient.HTTP_OPERATION_HELP);
                break;
            case 1:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_CALENDAR_TABLE);
                break;
            case 2:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_ATTENDANCE);
                break;
            case 3:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_APPROVAL);
                break;
            case 4:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_WORK);
                break;
            case 5:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_ANNOUNCEMENT);
                break;
            case 6:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_ACTION);
                break;
            case 7:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_VOTE);
                break;
            case 8:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_METTING);
                break;
            case 9:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_EVENT);
                break;
            case 10:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_CALENDAR);
                break;
            case 11:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_NEW_TEAM);
                break;
            case 12:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_TAG_TIP);
                break;
            case 13:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_TEAM_MANAGE);
                break;
            case 14:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_FRIEND);
                break;
            case 15:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_DRAFTS);
                break;
            case 16:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_APPROVAL_ADMIN);
                break;
            case 17:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_CONTACT);
                break;
            case 18:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_APP_WEEKLY_TIP);
                break;
            case 19:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_APP_WEEKLY_MAN);
                break;
            case 20:
                mWebView.loadUrl(AsynHttpClient.HTTP_HELP_APP_WEEKLY_TEMPLATE);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.help_ll_back:
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
